
source('utils.R')
source('read_dataset.R')

donors = list(
  d12_inds = open_dataset(glue('final_long/cycle=2012')) %>% filter(d_pcc > 0 | d_sp > 0 | d_jfc > 0) %>% pull(component),
  r12_inds = open_dataset(glue('final_long/cycle=2012')) %>% filter(r_pcc > 0 | r_sp > 0 | r_jfc > 0) %>% pull(component),
  d16_inds = open_dataset(glue('final_long/cycle=2016')) %>% filter(d_pcc > 0 | d_sp > 0 | d_jfc > 0) %>% pull(component),
  r16_inds = open_dataset(glue('final_long/cycle=2016')) %>% filter(r_pcc > 0 | r_sp > 0 | r_jfc > 0) %>% pull(component),
  d20_inds = open_dataset(glue('final_long/cycle=2020')) %>% filter(d_pcc > 0 | d_sp > 0 | d_jfc > 0) %>% pull(component),
  r20_inds = open_dataset(glue('final_long/cycle=2020')) %>% filter(r_pcc > 0 | r_sp > 0 | r_jfc > 0) %>% pull(component)
)

df12[, nat_quant_col := fct_collapse(nat_quant, "0" = c("0" , "0.1", "0.2" ,"0.3" ,"0.4","0.5", "0.6", "0.7", "0.8"))]
df16[, nat_quant_col := fct_collapse(nat_quant, "0" = c("0" , "0.1", "0.2" ,"0.3" ,"0.4","0.5", "0.6", "0.7", "0.8"))]
df20[, nat_quant_col := fct_collapse(nat_quant, "0" = c("0" , "0.1", "0.2" ,"0.3" ,"0.4","0.5", "0.6", "0.7", "0.8"))]

d12 = df12[component %chin% donors[['d12_inds']]]
d12[, subsq_giver := component %chin% donors[['d16_inds']]]
d12 = d12[, .(sq = mean(subsq_giver)), nat_quant_col][, class := 'Obama to Clinton'][, cycle := '2012 to 2016']

r12 = df12[component %chin% donors[['r12_inds']]]
r12[, subsq_giver := component %chin% donors[['r16_inds']]]
r12 = r12[, .(sq = mean(subsq_giver)), nat_quant_col][, class := 'Romney to Trump \'16'][, cycle := '2012 to 2016']

d20 = df12[component %chin% donors[['d12_inds']]]
d20[, subsq_giver := component %chin% donors[['d20_inds']]]
d20 = d20[, .(sq = mean(subsq_giver)), nat_quant_col][, class := 'Obama to Biden'][, cycle := '2012 to 2020']

r20 = df12[component %chin% donors[['r12_inds']]]
r20[, subsq_giver := component %chin% donors[['r20_inds']]]
r20 = r20[, .(sq = mean(subsq_giver)), nat_quant_col][, class := 'Romney to Trump \'20'][, cycle := '2012 to 2020']

save(d12, r12, d20, r20, file = 'summary_data/fig5.rda')
